﻿
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <title>Hello World</title>
        <meta charset="utf-8">
    </head>
    <body>
        <script type="text/javascript" src="cordova.js"></script>
        
        
        
        <script>
        	
        	var deviceName = "";
        	var deviceAddress = "";

					document.addEventListener('deviceready', function () {  
					  try{
					  	HuamaoPlugin.enable(function(){}, function(){});
						}catch(e){
							alert(e.message);
						}
					
					}, false);  

					function getDevices(){
						try{
							HuamaoPlugin.getDevices(getDeviceSucc, getDeviceError, []);
							document.getElementById("devices").innerHTML = "正在获取设备....";
						}catch(e){
							alert(e.message);
						}
					}
					
					function getDeviceSucc(obj){
					
		  			var dstr = JSON.stringify(obj);
					  
					  if(dstr != ""){
					  	dstr = dstr.substr(1,dstr.length - 2);	
					  }
					  
					  var ds = dstr.split(/\\n/);
	
					  for(var i=0;i<ds.length;i++){
	
							var dline = ds[i];
	
					  	if(dline == ""){
					  		continue;	
					  	}
					  	
					  	var dlinearr = dline.split('|');
					  	if(dlinearr.length == 1){
					  		continue;	
					  	}
	
					  	if(dlinearr[0].indexOf("YaoHua") > -1){
					  		
					  		// 电子秤这台蓝牙设备的名称和地址
					  		deviceName    = dlinearr[0];
					  		deviceAddress = dlinearr[1];
					  		
					  		//alert(this.deviceAddress);
					  		document.getElementById("devices").innerHTML = "</br><font color='blue'>已获取蓝牙称重设备地址：" + deviceAddress + "</font>";
	
					  		break;
					  	}
					  }
					  
					  if(deviceAddress == ""){
					  	document.getElementById("devices").innerHTML = "没有找到蓝牙称重设备";
					  }
						
					}
	
					function getDeviceError(obj) {
					  document.getElementById("devices").innerHTML = "查找蓝牙设备时插件异常：" + JSON.stringify(obj);
					}
					
					function recData(){
						
						if(deviceAddress == ""){
							alert("没有蓝牙设备。");	
							return;
						}
						
						try{
							var paramsObj = {'name':deviceName,'address':deviceAddress};
							HuamaoPlugin.recvData(function(){}, recDataError, [paramsObj]);
						}catch(e){
							alert(e.message);
						}
					}
					
					function getWeight(){
						try{
							HuamaoPlugin.getWeight(recDataSucc, recDataError);
							document.getElementById("weight").innerHTML = "请稍候 ....";
						}catch(e){
							alert(e.message);
						}
					}
					
					function recDataSucc(obj){
						
							var data = JSON.stringify(obj);
							if(data != ""){
						  	data = data.substr(1,data.length - 2);	
						  }
						  
							try{
								document.getElementById("weight").innerHTML = data + "kg";
							}catch(e){
								document.getElementById("weight").innerHTML = "程序异常：" + e.message;
							}
					}
					
					function recDataError(obj){
						 var data = JSON.stringify(obj);
						 document.getElementById("weight").innerHTML = "插件异常：" + data;
					}
        	
        </script>
        
        <input type="button" onclick="getDevices();" value="蓝牙设备" style="height:100px;width:200px;margin-top:100px;margin-left:50px;">
        <input type="button" onclick="recData();" value="开启称重" style="height:100px;width:200px;margin-top:100px;margin-left:50px;">
        <input type="button" onclick="getWeight();" value="获取重量" style="height:100px;width:200px;margin-top:100px;margin-left:50px;">
        <p> </p>
        ver:12:00
        <P>状态：<span id="devices">请获取蓝牙设备</span></p>
        <P>重量：<span id="weight">-1</span></p>
        <div></div>
    </body>
</html>
 